package es.wandererteam.tapakitty.management;

import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import android.graphics.Color;

public class ResourcesManager {
	// ---------------------------------------------
	// VARIABLES
	// ---------------------------------------------

	private static final ResourcesManager INSTANCE = new ResourcesManager();

	public Engine engine;
	public MainActivity activity;
	public BoundCamera camera;
	public VertexBufferObjectManager vbom;

	public int SCREEN_WIDTH = 0;
	public int SCREEN_HEIGHT = 0;

	public Font font;

	// ---------------------------------------------
	// ATLAS, TEXTURES & TEXTURE REGIONS --- SPLASH
	// ---------------------------------------------

	public ITextureRegion splash_region;
	private BitmapTextureAtlas splashTextureAtlas;

	// ---------------------------------------------
	// ATLAS, TEXTURES & TEXTURE REGIONS --- MENU
	// ---------------------------------------------
	public TextureRegion spikes;
	public TextureRegion backGroundMenu;
	public TextureRegion title;

	public ITextureRegion menu_background_region;
	public ITextureRegion play_region;
	public ITextureRegion options_region;

	public ITextureRegion size3;
	public ITextureRegion size4;

	private BuildableBitmapTextureAtlas menuTextureAtlas;

	// ---------------------------------------------
	// ATLAS, TEXTURES & TEXTURE REGIONS --- GAME
	// ---------------------------------------------

	public BuildableBitmapTextureAtlas gameTextureAtlas;

	public TextureRegion bellTextureRegion;
	public TextureRegion pawTextureRegion;
	public TextureRegion windowBackgroundTextureRegion;
	public TextureRegion particleTexture;

	public TiledTextureRegion regplusone;
	public TiledTextureRegion layertext;

	public Music catTouchSound;

	public TiledTextureRegion catTiledTextureRegion;
	public Font numberFont;
	public Font itemsNumberFont;
	public Font itemsNumberFont2;
	public Font itemsDescriptionFont;

	// ---------------------------------------------
	// ATLAS, TEXTURES & TEXTURE REGIONS --- SHOP
	// ---------------------------------------------
	public BuildableBitmapTextureAtlas shopTextureAtlas;
	public TextureRegion shopBackground1Texture;
	public TextureRegion shopBackground2Texture;
	
	public TextureRegion encabezadoTexture;
	
	public TextureRegion shopPopupTexture;
	public TextureRegion shopSection1Texture;
	
	public TiledTextureRegion iconsTiledTextureRegion;

	// ---------------------------------------------
	// ATLAS, TEXTURES & TEXTURE REGIONS --- OBJECT
	// ---------------------------------------------
	public BuildableBitmapTextureAtlas objectTextureAtlas;

	public TextureRegion objectBackgroundTexture;

	// ---------------------------------------------
	// CLASS LOGIC
	// ---------------------------------------------

	public void loadMenuResources() {
		loadMenuGraphics();
		loadMenuAudio();
		loadMenuFonts();
	}

	public void loadGameResources() {
		loadGameGraphics();
		loadGameFonts();
		loadGameAudio();
	}

	public void loadShopResources() {
		loadShopGraphics();
		loadShopFonts();
		loadShopAudio();
	}
	
	public void loadObjectResources() {
		loadObjectGraphics();
		loadObjectFonts();
		loadObjectAudio();
	}

	// ---------------------------------------------
	// LOAD GRAPHICS METHODS
	// ---------------------------------------------
	private void loadShopGraphics() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		shopTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		
		encabezadoTexture = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(shopTextureAtlas, activity,
						"encabezado_shop.png");
		shopBackground1Texture = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(shopTextureAtlas, activity,
						"backgroundShop.png");
		/*
		shopBackground2Texture = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(shopTextureAtlas, activity,
						"backgroundShopParrallax.png");
						*/
		
		
		iconsTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(shopTextureAtlas, activity,
						"icons_marco.png", 1, 6);
		try {
			this.shopTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.shopTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
		
	}

	private void loadObjectGraphics() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		objectTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		objectBackgroundTexture = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(objectTextureAtlas, activity,
						"objectSceneBackgroundNew.png");
		try {
			this.objectTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.objectTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
	}

	private void loadMenuGraphics() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		menuTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024,
				TextureOptions.BILINEAR);
		menu_background_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity,
						"menu_background.png");

		play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				menuTextureAtlas, activity, "play.png");
		options_region = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity, "options.png");

		spikes = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				menuTextureAtlas, activity, "spikes.png");
		backGroundMenu = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(menuTextureAtlas, activity,
						"backGroundMenu.png");
		title = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				menuTextureAtlas, activity, "title.png");
		try {
			this.menuTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));
			this.menuTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}

	}

	private void loadGameGraphics() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		gameTextureAtlas = new BuildableBitmapTextureAtlas(
				activity.getTextureManager(), 1024, 1024);

		catTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(gameTextureAtlas, activity,
						"kitthetwo2.png", 2, 1);

		bellTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(gameTextureAtlas, activity, "belly.png");

		pawTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(gameTextureAtlas, activity, "pawy.png");

		regplusone = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(gameTextureAtlas, activity,
						"plusoneanimation.png", 10, 1);

		

		windowBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(gameTextureAtlas, activity, "window.png");

	

		try {
			this.gameTextureAtlas
					.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
							0, 1, 0));

			this.gameTextureAtlas.load();
		} catch (final TextureAtlasBuilderException e) {
			Debug.e(e);
		}
	}

	// ---------------------------------------------
	// LOAD AUDIO METHODS
	// ---------------------------------------------

	public void loadMenuAudio() {

	}

	public void loadGameAudio() {

		try {
			catTouchSound = MusicFactory.createMusicFromAsset(
					this.engine.getMusicManager(), activity,
					"sounds/nyansound.ogg");

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void loadShopAudio() {

	}

	public void loadObjectAudio() {

	}

	// ---------------------------------------------
	// LOAD FONTS METHODS
	// ---------------------------------------------
	public void loadShopFonts() {
		FontFactory.setAssetBasePath("font/");
		final ITexture mainFontTexture = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		itemsNumberFont = FontFactory.createStrokeFromAsset(
				activity.getFontManager(), mainFontTexture,
				activity.getAssets(), "ARLRDBD.TTF", 25, true, Color.BLACK, 2,
				Color.rgb(0, 0, 0));
		itemsNumberFont.load();
		
		itemsDescriptionFont = FontFactory.createStrokeFromAsset(
				activity.getFontManager(), mainFontTexture,
				activity.getAssets(), "ARLRDBD.TTF", 16, true, Color.BLACK, 2,
				Color.rgb(0, 0, 0));
		itemsDescriptionFont.load();
		
		
	}

	public void loadObjectFonts() {

	}

	private void loadGameFonts() {
		FontFactory.setAssetBasePath("font/");
		final ITexture mainFontTexture = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		numberFont = FontFactory.createStrokeFromAsset(
				activity.getFontManager(), mainFontTexture,
				activity.getAssets(), "ARLRDBD.TTF", 50, true, Color.WHITE, 2,
				Color.rgb(255, 105, 180));
		numberFont.load();
	}

	private void loadMenuFonts() {
		FontFactory.setAssetBasePath("font/");
		final ITexture mainFontTexture = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		font = FontFactory.create(activity.getFontManager(), mainFontTexture,
				30);
		font.load();
	}

	// ---------------------------------------------
	// LOAD SPLASH SCENE
	// ---------------------------------------------

	public void loadSplashScreen() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		splashTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				splashTextureAtlas, activity, "splashW.png", 0, 0);
		splashTextureAtlas.load();
	}

	public void unloadSplashScreen() {
		splashTextureAtlas.unload();
		splash_region = null;
	}

	// ---------------------------------------------
	// GETTERS AND SETTERS
	// ---------------------------------------------

	public static void prepareManager(Engine engine, MainActivity activity,
			BoundCamera camera, VertexBufferObjectManager vbom) {
		getInstance().engine = engine;
		getInstance().activity = activity;
		getInstance().camera = camera;
		getInstance().vbom = vbom;
	}

	public TextureManager getTextureManagerResources() {
		return activity.getTextureManager();
	}

	public static ResourcesManager getInstance() {
		return INSTANCE;
	}

	public void unloadMenuTextures() {
		menuTextureAtlas.unload();
	}

	public void loadMenuTextures() {
		menuTextureAtlas.load();
	}

	public void unloadGameTextures() {
		// TODO (Since we did not create any textures for game scene yet)
	}

}